#include "NebeskoTelo.h"
#include <cmath>
using namespace std;


#define rad_per_deg     0.0174533f

void NebeskoTelo::crtajSferu(float radius, int br_segmenata)
{
float step = 90.0f / br_segmenata;
float x1, y1, z1, x2, y2, z2;
float theta, phi;



      for( theta = 0; theta < 90; theta += step )
      {
            glBegin(GL_QUAD_STRIP);
            x1 = radius * cos(theta * rad_per_deg);
            z1 = 0;
            y1 = radius * sin(theta * rad_per_deg);

            x2 = radius * cos( (theta+step) * rad_per_deg);
            z2 = 0;
            y2 = radius * sin( (theta+step) * rad_per_deg);

            glNormal3f(x1, y1, z1);
            glTexCoord2f(0, 0.5f + theta/180);
            glVertex3f(x1, y1, z1);

            glNormal3f(x2, y2, z2);
            glTexCoord2f(0, 0.5f + (theta+step)/180);
            glVertex3f(x2, y2, z2);

            for( phi = step; phi <= 360; phi += step)
            {
                  x1 = radius * cos(theta * rad_per_deg) * cos(phi * rad_per_deg);
                  z1 = radius * cos(theta * rad_per_deg) * sin(phi * rad_per_deg);
                  y1 = radius * sin(theta * rad_per_deg);

                  x2 = radius * cos((theta+step) * rad_per_deg) * cos(phi * rad_per_deg);
                  z2 = radius * cos((theta+step) * rad_per_deg) * sin(phi * rad_per_deg);
                  y2 = radius * sin((theta+step) * rad_per_deg);

                  glNormal3f(x1, y1, z1);
                  glTexCoord2f(phi/360, 0.5f + theta/180);
                  glVertex3f(x1, y1, z1);

                  glNormal3f(x2, y2, z2);
                  glTexCoord2f(phi/360, 0.5f + (theta+step)/180);
                  glVertex3f(x2, y2, z2);


            }

            glEnd();
      }


      for( theta = 0; theta > -90; theta -= step )
      {
            glBegin(GL_QUAD_STRIP);
            x1 = radius * cos(theta * rad_per_deg);
            z1 = 0;
            y1 = radius * sin(theta * rad_per_deg);

            x2 = radius * cos( (theta-step) * rad_per_deg);
            z2 = 0;
            y2 = radius * sin( (theta-step) * rad_per_deg);

            glNormal3f(x1, y1, z1);
            glTexCoord2f(0, 0.5f + theta/180);
            glVertex3f(x1, y1, z1);

            glNormal3f(x2, y2, z2);
            glTexCoord2f(0, 0.5f + (theta-step)/180);
            glVertex3f(x2, y2, z2);

            for( phi = step; phi <= 360; phi += step)
            {
                  x1 = radius * cos(theta * rad_per_deg) * cos(phi * rad_per_deg);
                  z1 = radius * cos(theta * rad_per_deg) * sin(phi * rad_per_deg);
                  y1 = radius * sin(theta * rad_per_deg);

                  x2 = radius * cos((theta-step) * rad_per_deg) * cos(phi * rad_per_deg);
                  z2 = radius * cos((theta-step) * rad_per_deg) * sin(phi * rad_per_deg);
                  y2 = radius * sin((theta-step) * rad_per_deg);

                  glNormal3f(x1, y1, z1);
                  glTexCoord2f(phi/360, 0.5f + theta/180);
                  glVertex3f(x1, y1, z1);
                  
                  glNormal3f(x2, y2, z2);
                  glTexCoord2f(phi/360, 0.5f + (theta-step)/180);
                  glVertex3f(x2, y2, z2);

            }

            glEnd();
      }
}

void NebeskoTelo::crtaj()
{
      glPushMatrix();
            if( tekstura )
                  tekstura->primeniTeksturu();

            glRotated( ugao_revolucije, 0, 1, 0 );
            glTranslated( poluprecnik_revolucije, 0, 0 );
			glRotated(ugao_rotacije,0, 1, 0);//oko svoje ose
            crtajTelo();

            for(int i = 0; i < broj_satelita; i++)
                  sateliti[i]->crtaj();

      glPopMatrix();
}

void NebeskoTelo::pomeri(int proteklo_vreme)
{
      ugao_revolucije += brzina_revolucije*proteklo_vreme;
	  ugao_rotacije += brzina_rotacije*proteklo_vreme;
      for(int i = 0; i < broj_satelita; i++)
            sateliti[i]->pomeri(proteklo_vreme);
}

void NebeskoTelo::rezervisiSatelite(int arg_broj_satelita)
{
      if( sateliti )
      {
            for(int i = 0; i < broj_satelita; i++)
                  delete sateliti[i];

            delete []sateliti;
      }
      sateliti = new NebeskoTelo*[max_satelita = arg_broj_satelita];
      for(int i = 0; i < arg_broj_satelita; i++)
            sateliti[i] = 0;

      broj_satelita = 0;
}

bool NebeskoTelo::dodajSatelit(NebeskoTelo *p)
{
      if( broj_satelita < max_satelita )
      {
            sateliti[broj_satelita++] = p;
            return true;
      }

      return false;
}

void Planeta::crtajTelo()
{
      glEnable(GL_LIGHTING);
      glPushMatrix();
            glScaled( poluprecnik, poluprecnik, poluprecnik );
            crtajSferu( 1, 5 );
      glPopMatrix();
}

void Zvezda::crtajTelo()
{
      glDisable(GL_LIGHTING);
      glPushMatrix();
            glScaled( poluprecnik, poluprecnik, poluprecnik );
			crtajSferu( 1, 5 );

      glPopMatrix();
}
